from django.shortcuts import render_to_response
from django.http import HttpResponse
from csvp.identity.models import Identity, Reservation

def index(request):
    openid = False
    identity = None
    if request.openid:
        identity = Identity.get_by_url(request.openid)
        openid = request.openid
    valuesDict = { 'openid' : request.openid, 'identity' : identity, 'next' : '/'}
    return render_to_response('main.html', valuesDict )


def event(request, event_id):
    openid = False
    identity = None
    myreservation = None
    mystatus      = None
    if request.openid:
        identity = Identity.get_by_url(request.openid)
        openid = request.openid
        # Find out if the user is going to this event
        # TODO: create function get_mystatus() out of this try block
        try:
            myreservation = Reservation.objects.get(identity=identity, event__exact=event_id)
            mystatus = myreservation.get_status()
        except Exception, e:
            pass
    reservations = {}
    for status in ['YES', 'NO', 'MAYBE']: 
        reservations[status] = Reservation.objects.filter(event=event_id, status=Reservation.__dict__[status])
    valuesDict = {  'openid'       : request.openid, 
                    'identity'     : identity,
                    'event_id'     : event_id,
                    'status'       : mystatus,
                    'reservations' : reservations,
                    'reservation'  : myreservation,
                    'yes_count'    : len(reservations['YES']),
                    'no_count'     : len(reservations['NO']),
                    'maybe_count'  : len(reservations['MAYBE']),
                    'YES'          : Reservation.YES, 
                    'NO'           : Reservation.NO,
                    'MAYBE'        : Reservation.MAYBE, 
                    'next'         : '/events/%s' % event_id 
                 }
    return render_to_response('event.html', valuesDict )


def user(request,username):
    identity = Identity.get_by_url(username)
    reservations = Reservation.objects.filter(identity=identity)
    return render_to_response('mine.html', {'openid': request.openid,
                                            'identity' : identity,
                                            'reservations' : reservations,
                                            'YES' : Reservation.YES,
                                            'NO' : Reservation.NO,
                                            'MAYBE' : Reservation.MAYBE,
                                            })

def next_works(request):
    return HttpResponse('?next= bit works. <a href="/">Home</a>')

def confirm(request, status, event_id):
    identity = Identity.get_by_url(request.openid)
    reservation = Reservation.objects.get_or_create(identity=identity,event=event_id)[0]
    reservation.status = status
    reservation.save()
    

def yes(request, event_id):
    confirm(request, Reservation.YES, event_id)
    return render_to_response('yesnomaybe.html', {'status': 'yes', 'event_id' : event_id})
def no(request, event_id):
    confirm(request, Reservation.NO, event_id)
    return render_to_response('yesnomaybe.html', {'status': 'no', 'event_id' : event_id})
def maybe(request, event_id):
    confirm(request, Reservation.MAYBE, event_id)
    return render_to_response('yesnomaybe.html', {'status': 'maybe', 'event_id' : event_id})    
